package com.yaoxiaowen.project2.test.jpush;

import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Layout;
import android.view.View;
import android.widget.EditText;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.tc.tickets.push.Push;
import com.yaoxiaowen.project2.R;
import com.yaoxiaowen.project2.base.BaseActivity;
import com.yaoxiaowen.project2.util.ToastUtils;

import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;

import butterknife.BindView;
import butterknife.OnClick;

public class PushMainActivity extends BaseActivity {



    @BindView(R.id.pushAliasEt)
    EditText pushAliasEt;
    @BindView(R.id.pushCtdProBar)
    View pushCtdProBar;
    @BindView(R.id.ctdTimeTv)
    TextView ctdTimeTv;

//    private TextView ctdTimeTv;

    public static final String TAG = "PushMainActivity";
    private static final boolean DEBUG = true;
    private static final boolean IS_SHOW = true;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_push_main;
    }

    @Override
    protected void initData() {
//        ctdTimeTv = (TextView)pushCtdProBar.findViewById(R.id.ctdTimeTv);
    }

    @Override
    protected void initView() {

    }

    @Override
    protected void initListener() {

    }

    @OnClick(R.id.pushSaveAlias)
    public void saveAliasOnClick(View view){
        String alias = pushAliasEt.getText().toString().trim();
        mShow.showToast(IS_SHOW, "alias = " + alias);
        Push mPush = new Push(getApplicationContext());
        mPush.setAlias(alias);
    }


//    private Toast paySuccToast;
    private int paySuccCountDown;
    private ScheduledExecutorService mScheduleService;
    private final Handler countDownHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what == 0x42) {
                mLog.i(DEBUG, TAG, "paySucc " + paySuccCountDown);
                if (paySuccCountDown > 0) {
//                    paySuccToast.setText("支付确认中...\t" + (paySuccCountDown--));
                    ctdTimeTv.setText("" + (paySuccCountDown++));
                } else {
                    ToastUtils.simpleShow(PushMainActivity.this, "结束");
                    pushCtdProBar.setVisibility(View.GONE);
                    mScheduleService.shutdownNow();
                }
            }
        }
    };

    @OnClick(R.id.pushShowToast)
    public void showToastOnClick(View view){
        paySuccCountDown = 10;
        ctdTimeTv.setText("" + paySuccCountDown);
        pushCtdProBar.setVisibility(View.VISIBLE);
//        paySuccToast = Toast.makeText(this, "支付确认中...\t" + paySuccCountDown, Toast.LENGTH_LONG);
//        paySuccToast.show();
        paySuccCountDown--;

        mScheduleService = Executors.newScheduledThreadPool(1);
        mScheduleService.scheduleWithFixedDelay(new Runnable() {
            @Override
            public void run() {
                countDownHandler.sendEmptyMessage(0x42);
            }
        }, 0, 1, TimeUnit.SECONDS);
    }
}
